package com.factors.HuangHePing.AttendanceAvg;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class AttendanceMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    private static final String A = "90-100";
    private static final String B = "80-90";
    private static final String C = "70-80";
    private static final String D = "60-70";

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        String[] columns = value.toString().split(",");
        if (columns.length >= 20) {
            // 解析出勤率和考试分数，假设输入数据格式正确
            int attendance = Integer.parseInt(columns[1]);
            int examScore = Integer.parseInt(columns[19]);

            String range;
            if (attendance >= 90 && attendance <= 100) {
                range = A;
            } else if (attendance >= 80 && attendance < 90) {
                range = B;
            } else if (attendance >= 70 && attendance < 80) {
                range = C;
            } else if (attendance >= 60 && attendance < 70) {
                range = D;
            } else {
                // 如果出勤率不在预期范围内，则跳过该记录
                return;
            }

            // 输出键值对：出勤率区间 -> 考试分数
            context.write(new Text(range), new IntWritable(examScore));
        }
    }
}